home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / Folder・Icon・Opener 1.0.1.sit / Folder•Icon•Opener 1.0.1 / source code / sources / FIOpen.send.c < prev   
Text File  |  1996-05-05  |  9KB  |  283 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * FIOpen.send.c
  4.  *--------------------------------------------------------------
  5.  */
  6. #include <Files.h>
  7. #include <Processes.h>
  8. #include <AppleEvents.h>
  9. #include <AERegistry.h>
  10.  
  11. #include "FIOpen.h"
  12.  
  13. /* static functions */
  14. static OSErr SendOpenDocToApp(ProcessSerialNumberPtr, const FSSpecPtr);
  15. static OSErr SendMyAppleEvent(AEAddressDesc *, AEDescList *, const AEEventID);
  16. static OSErr LaunchAppWithDoc(const FSSpecPtr app, const FSSpecPtr doc);
  17. static OSErr LaunchAppWithAEDesc(const FSSpecPtr, AEDescList *);
  18. static OSErr MakeMyDocDescList(const FSSpecPtr, AEDescList *);
  19. static OSErr FindAProcessByFSS(const FSSpecPtr, ProcessSerialNumberPtr);
  20.  
  21. /*
  22.  *--------------------------------------------------------------
  23.  * PassFileToApp
  24.  *--------------------------------------------------------------
  25.  *    send the file spec to the application
  26.  *--------------------------------------------------------------
  27.  */
  28. OSErr PassFileToApp(const FSSpecPtr theApp, const FSSpecPtr theDoc)
  29. {
  30.     ProcessSerialNumber    appPSN;
  31.     OSErr    result;    
  32.  
  33.     result = FindAProcessByFSS(theApp, &appPSN);
  34.     if (result == noErr) {
  35.         /* the process is already running */
  36.         result = SendOpenDocToApp(&appPSN, theDoc);
  37.         if (result == noErr) {
  38.             /* let the application be in front */
  39.             result = SetFrontProcess(&appPSN);
  40.         }
  41.     } else {
  42.         /* let the icon file to be opened */
  43.         result = LaunchAppWithDoc(theApp, theDoc);
  44.     }
  45.     if (result != noErr) {
  46.         AEErrorAlert(theDoc->name);
  47.     }
  48.     return (result);
  49. }
  50. /*
  51.  *--------------------------------------------------------------
  52.  * SendOpenDocToApp
  53.  *--------------------------------------------------------------
  54.  *    send Open Document AppleEvent to the designated application
  55.  *--------------------------------------------------------------
  56.  */
  57. static OSErr SendOpenDocToApp(
  58.     ProcessSerialNumberPtr thePSN,
  59.     const FSSpecPtr theDocSpec)
  60. {
  61.     AEAddressDesc    targetAddress = {typeNull, nil};
  62.     AEDescList        myDocList = {typeNull, nil};
  63.     OSErr            result;
  64.  
  65.     /* create a descriptor of the application with its ProcessSerialNumber */
  66.     result = AECreateDesc(typeProcessSerialNumber, thePSN,
  67.             sizeof(ProcessSerialNumber), &targetAddress);
  68.  
  69.     /* create a descriptor list of the document */
  70.     if (result == noErr) {
  71.         result = MakeMyDocDescList(theDocSpec, &myDocList);
  72.     }
  73.  
  74.     /* send an AppleEvent to the application along with the documet list */
  75.     if (result == noErr) {
  76.         result = SendMyAppleEvent(&targetAddress, &myDocList, kAEOpenDocuments);
  77.     }
  78.  
  79.     /* cleanup */
  80.     AEDisposeDesc(&targetAddress);
  81.     AEDisposeDesc(&myDocList);
  82.  
  83.     return (result);
  84. }
  85. /* 
  86.  *--------------------------------------------------------------
  87.  * SendMyAppleEvent
  88.  *--------------------------------------------------------------
  89.  *    send AppleEvent with the target application's address and
  90.  *    the descriptor of the file
  91.  *--------------------------------------------------------------
  92.  */
  93. static OSErr SendMyAppleEvent(
  94.     AEAddressDesc    *theTargetAddress,
  95.     AEDescList        *theDocDesc,
  96.     const AEEventID    theAEKind)
  97. {
  98.     AppleEvent    myAppleEvent = {typeNull, nil};
  99.     OSErr        result;
  100.     
  101.     /* create an AppleEvent record to be sent */
  102.     result = AECreateAppleEvent(kCoreEventClass, theAEKind, theTargetAddress,
  103.                 kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  104.  
  105.     /* put the document list into the AppleEvent */
  106.     if (result == noErr) {
  107.         result = AEPutParamDesc(&myAppleEvent, keyDirectObject, theDocDesc);
  108.     }
  109.  
  110.     /* now send the AppleEvent */
  111.     if (result == noErr) {
  112.         AppleEvent    nullRepy  = {typeNull, nil};
  113.         result = AESend(&myAppleEvent, &nullRepy,
  114.             kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  115.             kAENormalPriority, kAEDefaultTimeout, nil, nil);
  116.     }
  117.  
  118.     /* cleanup */
  119.     AEDisposeDesc(theTargetAddress);
  120.     AEDisposeDesc(theDocDesc);
  121.     AEDisposeDesc(&myAppleEvent);
  122.  
  123.     return (result);
  124. }
  125. /*
  126.  *--------------------------------------------------------------
  127.  * LaunchAppWithDoc
  128.  *--------------------------------------------------------------
  129.  *    launch application with 'odoc' AppleEvent
  130.  *--------------------------------------------------------------
  131.  */
  132. static OSErr LaunchAppWithDoc(const FSSpecPtr theAppSpec, const FSSpecPtr theDocSpec)
  133. {
  134.     AEDescList    itsDocList = {typeNull, nil};
  135.     OSErr        result;
  136.  
  137.     result = MakeMyDocDescList(theDocSpec, &itsDocList);
  138.     if (result == noErr) {
  139.         result = LaunchAppWithAEDesc(theAppSpec, &itsDocList);
  140.     }
  141.     return (result);
  142. }
  143. /* 
  144.  *--------------------------------------------------------------
  145.  * LaunchAppWithAEDesc
  146.  *--------------------------------------------------------------
  147.  *    Launch specified application with doc list
  148.  *    FSSpec should be the application spec and 
  149.  *    AEDescList should be the document descriptor
  150.  *--------------------------------------------------------------
  151.  */
  152. static OSErr LaunchAppWithAEDesc(const FSSpecPtr theAppSpec, AEDescList *theDocDesc)
  153. {
  154.     LaunchParamBlockRec    launchPrm;
  155.     ProcessSerialNumber    myPSN;
  156.     AppleEvent            myAppleEvent = {typeNull, nil};
  157.     AEDesc                targetDesc = {typeNull, nil};
  158.     AEDesc                launchDesc = {typeNull, nil};
  159.     OSErr                result;
  160.  
  161.     /* get the ProcessSerialNumber of myself */
  162.     result = GetCurrentProcess(&myPSN);
  163.  
  164.     /* create an address descriptor of myself */
  165.     if (result == noErr) {
  166.         result = AECreateDesc(typeProcessSerialNumber, (Ptr)&myPSN,
  167.             sizeof(ProcessSerialNumber), &targetDesc);
  168.     }
  169.  
  170.     /* create an AppleEvent record to be sent */
  171.     if (result == noErr) {
  172.         result = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &targetDesc,
  173.             kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  174.     }
  175.  
  176.     /* put the document list into the AppleEvent */
  177.     if (result == noErr) {
  178.         result = AEPutParamDesc(&myAppleEvent, keyDirectObject, theDocDesc);
  179.     }
  180.  
  181.     /* coerce (convert) the AppleEvent into launching descriptor */
  182.     if (result == noErr) {
  183.         result = AECoerceDesc(&myAppleEvent, typeAppParameters, &launchDesc);
  184.     }
  185.  
  186.     /* set the launch parameters */
  187.     if (result == noErr) {
  188.         HLock(launchDesc.dataHandle);
  189.  
  190.         launchPrm.launchBlockID   = extendedBlock;
  191.         launchPrm.launchEPBLength = extendedBlockLen;
  192.         launchPrm.launchFileFlags = launchNoFileFlags;
  193.         launchPrm.launchAppSpec   = theAppSpec;
  194.         launchPrm.launchControlFlags = launchContinue;
  195.         launchPrm.launchAppParameters = (AppParametersPtr)(*launchDesc.dataHandle);
  196.  
  197.         /* now launch it! */
  198.         result = LaunchApplication(&launchPrm);    
  199.  
  200.         HUnlock(launchDesc.dataHandle);
  201.     }
  202.  
  203.     /* settlements */
  204.     AEDisposeDesc(theDocDesc);
  205.     AEDisposeDesc(&targetDesc);
  206.     AEDisposeDesc(&myAppleEvent);
  207.     AEDisposeDesc(&launchDesc);
  208.     return (result);
  209. }
  210. /* 
  211.  *--------------------------------------------------------------
  212.  * MakeMyDocDescList
  213.  *--------------------------------------------------------------
  214.  *    make AEDescList from FSSpec; its content is just one FSSpec
  215.  *--------------------------------------------------------------
  216.  */
  217. static OSErr MakeMyDocDescList(const FSSpecPtr theSpec, AEDescList *theList)
  218. {
  219.     AEDesc         itsDocDesc = {typeNull, nil};
  220.     AliasHandle    itsAlias;
  221.     Size        itsSize;
  222.     OSErr         result = noErr;
  223.  
  224.     /* create a new document list */
  225.     result = AECreateList(nil, 0, false, theList);    /* new description */
  226.  
  227.     /* create an alias record of the document */
  228.     if (result == noErr) {
  229.         result = NewAlias(nil, theSpec, &itsAlias);
  230.     }
  231.     if (result == noErr) {
  232.         HLock((Handle)itsAlias);
  233.         itsSize = GetHandleSize((Handle)itsAlias);
  234.  
  235.         /* create a descriptor of the document alias */
  236.         result = AECreateDesc(typeAlias, (Ptr)*itsAlias, itsSize, &itsDocDesc);
  237.  
  238.         /* put the alias descriptor into the document list */
  239.         if (result == noErr) {
  240.             result = AEPutDesc(theList, 0, &itsDocDesc);
  241.         }
  242.         /* dispose the alias descriptor and the alias record */
  243.         AEDisposeDesc(&itsDocDesc);
  244.         DisposeHandle((Handle)itsAlias);
  245.     }
  246.     return (result);
  247. }
  248. /*
  249.  *--------------------------------------------------------------
  250.  * FindAProcessByFSS
  251.  *--------------------------------------------------------------
  252.  *    find the ProcessSerialNumber of the given FSSpec
  253.  *--------------------------------------------------------------
  254.  */
  255. static OSErr FindAProcessByFSS(
  256.     const FSSpecPtr theSpec, 
  257.     ProcessSerialNumberPtr thePSN)
  258. {
  259.     ProcessInfoRec        thisInfo;
  260.     FSSpec                specRec;
  261.     FSSpecPtr            thisSpec = &specRec;
  262.     Str31                itsName;
  263.  
  264.     /* initialize the ProcessSerialNumber */
  265.     thePSN->lowLongOfPSN = thePSN->highLongOfPSN = kNoProcess;
  266.  
  267.     /* search the processes */
  268.     while (GetNextProcess(thePSN) != procNotFound) {
  269.         thisInfo.processInfoLength = sizeof(ProcessInfoRec);
  270.         thisInfo.processName       = itsName;
  271.         thisInfo.processAppSpec    = thisSpec;
  272.         GetProcessInformation(thePSN, &thisInfo);
  273.  
  274.         /* compare the FSSpecs */
  275.         if (thisSpec->vRefNum == theSpec->vRefNum
  276.          && thisSpec->parID == theSpec->parID
  277.          && EqualString(thisSpec->name, theSpec->name, false, true)) {
  278.             return (noErr);
  279.         }
  280.     }
  281.     return (procNotFound);
  282. }
  283.